package be.vdabantwerpen.gow.services;

import java.util.List;

import be.vdabantwerpen.gow.dao.UserDAO;
import be.vdabantwerpen.gow.dao.UserDAOImpl;
import be.vdabantwerpen.gow.entities.GOWUser;
import be.vdabantwerpen.gow.exceptions.UserNotFoundException;

public class UserServiceImpl implements UserService {

	private final UserDAO userDAO = new UserDAOImpl();

	@Override
	public void create(GOWUser user) {
		userDAO.create(user);

	}

	@Override
	public GOWUser read(int userId) {
            GOWUser user = userDAO.read(userId);
            if(user == null){
                throw new UserNotFoundException();
            }
            return user;
	}

	@Override
	public void update(GOWUser user) {
		userDAO.update(user);
	}

	@Override
	public void delete(GOWUser user) {
		userDAO.delete(user);

	}

	@Override
	public List<GOWUser> findAll() {
            List<GOWUser> users = userDAO.findAll();
            if(users == null || users.isEmpty()){
                throw new UserNotFoundException();
            }
            return users;
	}
        
        @Override
        public GOWUser findByName(String userName){
            List<GOWUser> users = findAll();
            for(GOWUser user : users){
                if(user.getUserName().equalsIgnoreCase(userName)){
                    return user;
                }
            }
            throw new UserNotFoundException();
        }

}
